package com.situ.ymall.admin.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.ymall.admin.entity.APIResult;
import com.situ.ymall.admin.util.TokenUtil;
import com.situ.ymall.admin.util.TokenUtil.Token;
import com.situ.ymall.admin.util.VerCodeUtil;

@Controller
@CrossOrigin(origins = {"*"})
public class VercodeController {
	/**
	 * 返回一张图片
	 */
	
	@GetMapping("/vercode")
	@ResponseBody
	public APIResult vercode() {
		// 1.生成随机字符串
		String vercode = VerCodeUtil.randomCode(4);
		// 2.加密到Token中 	JWT
		String strToken = TokenUtil.createToken(vercode);
		
		APIResult apiResult = new APIResult();
		apiResult.setMsg(strToken);
		apiResult.setStatus(200);
		return apiResult;
	}
	
	@GetMapping("/vercode/{token}")//路径参数
	public void vercode(@PathVariable("token")String strToken ,
			HttpServletResponse response) {
		//解密
		Token token = TokenUtil.parseToken(strToken);
		String vercode = token.getCode();		
		VerCodeUtil.createVerCode(response, vercode);	
	}
}
